using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using HgAdapter;
using HogoLogic.HgFileSystem;

namespace FileSystemView
{
	class HogoFileListItemVisualizer
	{
		public ListViewItem[] MakeListViewItemArray(HgDirectoryInfo directoryInfo)
		{
			List<HgFileInfo> files = directoryInfo.GetFiles();
			ListViewItem[] result = new ListViewItem[files.Count];

			for (int i = 0; i < files.Count; i++)
			{
				result[i] = MakeListViewItem(files[i]);
			}

			return result;
		}

		public HgFileInfo GetFileForListViewItem(ListViewItem listViewItem)
		{
			return listViewItem.Tag as HgFileInfo;
		}

		private static ListViewItem MakeListViewItem(HgFileInfo curFile)
		{
			ListViewItem result = new ListViewItem();
			
			result.Text = Path.GetFileNameWithoutExtension(curFile.Name);
			result.SubItems.Add(Path.GetExtension(curFile.Name).TrimStart('.'));
			int? imageIndex = GetImageIndex(curFile.VersionStatus);
			if (imageIndex.HasValue)
				result.ImageIndex = imageIndex.Value;
			result.Tag = curFile;
			
			return result;
		}

		private static int? GetImageIndex(VersionStatus status)
		{
			switch(status)
			{
				case VersionStatus.Empty:
					return null;
				case VersionStatus.Unchanged:
					return 0;
				case VersionStatus.Changed:
					return 2;
				case VersionStatus.Unversioned:
					return 1;
				case VersionStatus.Missed:
					return null;
				case VersionStatus.Added:
					return 3;
				default:
					return null;
			}
		}
	}
}
